package com.wsz.security;

import com.wsz.security.pojo.Role;
import com.wsz.security.pojo.User;
import com.wsz.security.service.RoleService;
import com.wsz.security.service.UserService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.ArrayList;

@MapperScan("com.wsz.security.mapper")
@SpringBootApplication
public class  SecurityApplication {

    public static void main(String[] args) {
        SpringApplication.run(SecurityApplication.class, args);
    }

    CommandLineRunner run(UserService userService,RoleService roleService,PasswordEncoder passwordEncoder){
        return args -> {
            roleService.save(new Role(null,"ROLE_USER"));
            roleService.save(new Role(null,"ROLE_MANAGER"));
            roleService.save(new Role(null,"ROLE_ADMIN"));
            roleService.save(new Role(null,"ROLE_SUPER_ADMIN"));

            String password = passwordEncoder.encode("123");

            userService.save(new User(null,"wsz","breeze",password,new ArrayList<>()));
            userService.save(new User(null,"fxc","wy",password,new ArrayList<>()));
            userService.save(new User(null,"qlc","yyzq",password,new ArrayList<>()));
            userService.save(new User(null,"ww","nh",password,new ArrayList<>()));

            userService.addRoleToUser("breeze","ROLE_SUPER_ADMIN");
            userService.addRoleToUser("wy","ROLE_ADMIN");
            userService.addRoleToUser("yyzq","ROLE_MANAGER");
            userService.addRoleToUser("nh","ROLE_USER");
        };
    }

}
